// pages/add/skills/skills.js
Page({
  data: {
    isEdit: false,
    id: "",
    bottomHeight: getApp().globalData.bottomHeight,
    fileList: [],
    imgIds: [],
    title: "",
    content: "",
    tel: "",
    isSync: true,
    btn_status: true
  },
  onLoad: function (options) {
    if (options.edit == 1) {
      this.setData({
        isEdit: true,
        id: options.id
      });
      this.getDetail();
    }
  },
  getDetail() {
    getApp().globalData.http.detail({
      id: this.data.id
    }).then(res => {
      let tmp = res.data;
      let imgs = [];
      tmp.imgUrls = tmp.imgUrls.split(",");
      tmp.imgUrls.forEach(item => {
        imgs.push({
          url: item
        });
      });
      this.setData({
        fileList: imgs,
        imgIds: tmp.imgIds.split(","),
        title: tmp.title,
        content: tmp.content,
        tel: tmp.tel,
        isSync: tmp.isSync == 1 ? true : false
      });
    });
  },
  upload(e) {
    getApp().globalData.util.imgsUpload(this, e, this.data.fileList, this.data.imgIds);
  },
  delete(e) {
    let i = e.detail.index;
    let fileList = this.data.fileList;
    let imgIds = this.data.imgIds;
    fileList.splice(i, 1);
    imgIds.splice(i, 1);
    this.setData({
      fileList: fileList,
      imgIds: imgIds
    });
  },
  syncHandle() {
    this.setData({
      isSync: !this.data.isSync
    });
  },
  submit() {
    let params = {
      type: 7,
      title: this.data.title,
      content: this.data.content,
      imgIds: this.data.imgIds.join(","),
      tel: this.data.tel,
      isSync: this.data.isSync ? 1 : 0,
    };
    if (this.data.btn_status) {
      if (!params.imgIds) {
        getApp().globalData.vant.toast("您还没有上传图片");
      } else if (!params.title) {
        getApp().globalData.vant.toast("技能标签不能为空");
      } else if (params.title.length > 6) {
        getApp().globalData.vant.toast("技能标签不能超过6个字");
      } else if (!params.content) {
        getApp().globalData.vant.toast("服务介绍不能为空");
      } else if (!params.tel) {
        getApp().globalData.vant.toast("联系电话不能为空");
      } else if (!getApp().globalData.reg.tel.test(params.tel)) {
        getApp().globalData.vant.toast("联系电话格式错误");
      } else {
        this.setData({
          btn_status: false
        });
        if (!this.data.isEdit) {
          getApp().globalData.http.squarePost(params).then(res => {
            getApp().globalData.vant.toast({
              type: "success",
              forbidClick: true,
              message: "发布成功",
              onClose: () => {
                wx.switchTab({
                  url: "/pages/home/home",
                  success: function (e) {
                    var page = getCurrentPages().pop();
                    if (page == undefined || page == null) return;
                    page.reset();
                  }
                });
              }
            });
          }).catch(e => {
            this.setData({
              btn_status: true
            });
          });
        } else {
          params.id = this.data.id
          getApp().globalData.http.squareUpdate(params).then(res => {
            getApp().globalData.vant.toast({
              type: "success",
              forbidClick: true,
              message: "保存成功",
              onClose: () => {
                wx.navigateBack({
                  success: function (e) {
                    var page = getCurrentPages().pop();
                    if (page == undefined || page == null) return;
                    page.reset();
                  }
                });
              }
            });
          }).catch(e => {
            this.setData({
              btn_status: true
            });
          });
        }
      }
    }
  }
})